package com.esm.sm800.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.esm.sm800.R;
import com.esm.sm800.database.manager.SystemCenter;
import com.esm.sm800.util.Camera2Manager;

import zuo.biao.library.base.BaseActivity;

//机针识别调试
public class JzsbceActivity extends BaseActivity implements View.OnClickListener {

    private final String TAG = "JzsbceActivity";

    private ImageView iv_pic;
    TextView tv_sbjg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_jzsbce);

        iv_pic = (ImageView) findViewById(R.id.iv_pic);
        tv_sbjg = (TextView) findViewById(R.id.tv_sbjg);
        findViewById(R.id.tv_dksxt).setOnClickListener(this);
        findViewById(R.id.tv_shibie).setOnClickListener(this);
        findViewById(R.id.tv_back).setOnClickListener(this);

        tv_sbjg.setText("");

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public static final int FINISH_SB = 1101;
    public static final int DOPAIZHAO = 1103;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == FINISH_SB) {
                dismissProgressDialog();
                if (_len >= 0) {
                    tv_sbjg.setText(getString(R.string.changduwei) + _len);
                    showShortToast(getString(R.string.changduwei) + _len);
                    SystemCenter.getInstance().needlelen = _len;
                } else {
                    tv_sbjg.setText(getString(R.string.changduwei));
                    showShortToast(getString(R.string.yichangchucuo));
                }
            } else if (msg.what == DOPAIZHAO) {
                Camera2Manager.get().rePz();//不能在线程里调用
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_dksxt:
                onShibie();
                break;
            case R.id.tv_back:
                setResult(RESULT_OK);
                finish();
                break;
        }
    }

    int _len = 0;//识别的长度-像素
    boolean _isshibieing = false;

    private void onShibie() {

        if (_isshibieing) {
            return;
        }
        tv_sbjg.setText("");
        _isshibieing = true;
        showProgressDialog(getString(R.string.zzsbiezhong));

        new Thread() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(DOPAIZHAO);
                while (true) {
                    int t = 0;
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    t++;
                    _len = Camera2Manager.get().getNeedelen();
                    if (_len >= 0 || t > 5) {
                        break;
                    }
                }

                _isshibieing = false;
                mHandler.sendEmptyMessage(FINISH_SB);
            }
        }.start();

    }

    protected void onResume() {
        super.onResume();
        Camera2Manager.get()._imageView = iv_pic;
    }

    @Override
    protected void onPause() {
        super.onPause();
        Camera2Manager.get()._imageView = null;

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            return;
        }

    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {

    }
}
